#region Copyright (C) 1998-2009 Autumoon Lab, all rights reserved.
/* *************************************************************
 * Solution:    Autumoon Code Library - Standard Edition
 * 
 * Project:     Database Controller
 * 
 * Description: The control tool class of Language.
 * 
 * Auther:      ZeroCool.
 * 
 * Date:        March 23, 2008.
 * 
 * Copyright (C) 1998-2009 Autumoon Lab, all rights reserved.
 * ************************************************************/
#endregion

#region References
using System;
using System.Data;
using System.Data.Odbc;

using Autumoon.CodeLibrary.CommonFoundation;
#endregion

namespace Autumoon.CodeLibrary.DatabaseController
{
    public class LanguageController
    {
        // Properties and fields.
        #region Properties and fields
        #endregion

        // Constructors.
        #region Constructors
        #endregion

        // Public methods.
        #region Public Methods
        /// <summary>
        /// Insert a language category.
        /// </summary>
        /// <param name="language">The language category.</param>
        public static void Insert(string language)
        {
            using (OdbcConnection connection = new OdbcConnection(ConstantString.ConnectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = "Insert Into " + ConstantString.LanguageTableName + "(Language) Values('" + language + "');";

                connection.Open();

                if (command.ExecuteNonQuery() != 1)
                {
                    throw new InvalidOperationException("Insert language operating has failed.");
                }
            }
        }

        /// <summary>
        /// Delete an existing language.
        /// </summary>
        /// <param name="language">The existing language.</param>
        public static void Delete(string language)
        {
            using (OdbcConnection connection = new OdbcConnection(ConstantString.ConnectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = "Delete From " + ConstantString.LanguageTableName + " Where Language = '" + language + "';";

                connection.Open();

                command.ExecuteNonQuery();
            }
        }

        /// <summary>
        /// Get all languages.
        /// </summary>
        /// <returns>The dataset of all languages.</returns>
        public static DataSet GetAllLanguages()
        {
            using (OdbcConnection connection = new OdbcConnection(ConstantString.ConnectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = "Select * From " + ConstantString.LanguageTableName + " Order By ID Asc;";

                connection.Open();

                DataSet languages = new DataSet();
                OdbcDataAdapter adapter = new OdbcDataAdapter(command);

                adapter.Fill(languages);

                return languages;
            }
        }

        /// <summary>
        /// Check whether the language exists or not.
        /// </summary>
        /// <param name="langaugeName">The language's name.</param>
        /// <returns>The language exists or not.</returns>
        public static bool CheckLanguageExists(string langaugeName)
        {
            using (OdbcConnection connection = new OdbcConnection(ConstantString.ConnectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = "Select * From " + ConstantString.LanguageTableName + " Where Language = '" + langaugeName + "';";

                connection.Open();

                OdbcDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    if (reader["Language"].ToString() == langaugeName)
                    {
                        return true;
                    }
                }

                return false;
            }
        }
        #endregion

        // Internal methods.
        #region Internal Methods
        #endregion

        // Protected methods.
        #region Protected Methods
        #endregion

        // Private methods.
        #region Private Methods
        #endregion
    }
}